$('#table_list').DataTable({
    dom: '<"top">rt<"bottom"flip><"clear">',
    language: {
        "sUrl":'../static/plugins/datatables/language.json'
    },
    "searching": false,
    "processing": true,
    "serverSide": true,
    ajax:{
        url:'/brand-category/get-brand-category',
        type:'post',
        error:getError
    },
    aoColumns: [
        { "data": "num", "bSortable": false},
        { "data": "name", "bSortable": false },
        { "data": "sort", "bSortable": true },
        { "data": "use_flg", "bSortable": true },
        { "data": "", "bSortable": false}
    ],
    columnDefs: [
    	{
            "targets": [0],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
                return data;
            }
        },
        {
            "targets": [3],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
            	return '<span class="switch" data-id="'+ full.id +'" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
            }
        },
        {
            "targets": [4],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
                return '<a href="javascript:" onclick="operBrandCategory('+ full.id +',\''+ full.name +'\','+ full.sort +',1,$(this))">编辑</a>';
            }
        },
    ],
    order: [[ 2, "asc"]]
});

function operBrandCategory(id,name,sort,type,where){
	if(type == 1){
		if($('#table_list').find('input[name=brand_category_name]').val() != undefined){
			$.jBox.error('当前已处于编辑状态，请先保存或取消', '错误');
			return false;
		}
		var name_html = '<input name="brand_category_name" style="width:100%;height:20px;height:30px;" class="form-control" type="text" value="'+name+'">';
		var sort_html = '<input name="brand_category_sort" style="height:30px;" class="form-control" type="text" value="'+sort+'">';
		where.parents('tr').find('td:eq(1)').html(name_html);
		where.parents('tr').find('td:eq(2)').html(sort_html);
		var html = '<a href="javascript:" onclick="saveBrandCategory('+ id +',$(this))">保存</a> | <a href="javascript:" onclick="operBrandCategory('+ id +',\''+ name +'\','+ sort +',2,$(this))" class="text-danger">取消</a>';
		where.parent('td').html(html);
	}else{
		where.parents('tr').find('td:eq(1)').html(name);
		where.parents('tr').find('td:eq(2)').html(sort);
		var html = '<a href="javascript:" onclick="operBrandCategory('+ id +',\''+ name +'\','+ sort +',1,$(this))">编辑</a> | <a href="javascript:" class="text-danger" onclick="deleteBrandCategory('+ id +')">删除</a>';
		where.parent('td').html(html);
	}
}

function cancelBrandCategory(where){
	where.parents('tr').remove();
}

function saveBrandCategory(id,where){
	var name = where.parents('tr').find('input[name=brand_category_name]').val();
	var sort = where.parents('tr').find('input[name=brand_category_sort]').val();
	$.post('/brand-category/save-brand-category',{id:id,name:name,sort:sort},success,'json');
    return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () { table_list_reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
}

function changeBrandCategoryStatus(id,use_flg){
	title = '确定停用此品牌街品牌分类？';
	if(use_flg == 1){
		title = '确定启用此品牌街品牌分类？';
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/brand-category/change-brand-category-status',{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	$.jBox.close();
            if(use_flg == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
        }
    });
}

function table_list_reload(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
}

$('.add-brand-category').on('click',function(){
	if($('#table_list').find('input[name=brand_category_name]').val() != undefined){
		$.jBox.error('当前已处于编辑状态，请先编辑或取消再添加新分类', '错误');
		return false;
	}
	var add_html = '';
	add_html = '<tr class="odd">';
	add_html += '<td></td>';
	add_html += '<td><input name="brand_category_name" style="width:100%;height:30px;" class="form-control" type="text" value=""></td>';
	add_html += '<td><input name="brand_category_sort" style="height:30px;" class="form-control" type="text" value=""></td>';
	add_html += '<td>--</td>';
	add_html += '<td><a href="javascript:" onclick="saveBrandCategory(0,$(this))">保存</a> | <a href="javascript:" onclick="cancelBrandCategory($(this))" class="text-danger">取消</a></td>';
	add_html += '</tr>';
	$('#table_list').find('tbody').append(add_html);
});

$('table').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	changeBrandCategoryStatus(id,use_flg);
});